package com.ruoyi.mes.service.impl;

import java.util.Comparator;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.LongMenUtils;
import com.ruoyi.mes.vo.buy.BuyInHouVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IBuyInhouService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购入库Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class BuyInhouServiceImpl implements IBuyInhouService
{
    @Autowired
    private HouInfoMapper houInfoMapper;

    @Autowired
    private MtrStockMapper mtrStockMapper;

    @Autowired
    private BuyOrderMapper buyOrderMapper;

    @Autowired
    private BuyInhouMapper buyInhouMapper;

    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    /**
     * 查询采购入库
     *
     * @param id 采购入库主键
     * @return 采购入库
     */
    @Override
    public BuyInhou selectBuyInhouById(Long id)
    {
        return buyInhouMapper.selectBuyInhouById(id);
    }

    /**
     * 查询采购入库列表
     *
     * @param buyInhou 采购入库
     * @return 采购入库
     */
    @Override
    public List<BuyInhou> selectBuyInhouList(BuyInhou buyInhou)
    {
        List<BuyInhou> buyInhous = buyInhouMapper.selectBuyInhouList(buyInhou);
        if (buyInhous.size() != 0){
            for (BuyInhou inhou : buyInhous) {
                BuyOrder buyOrder = buyOrderMapper.selectBuyOrderById(inhou.getBuyId());
                inhou.setBuyOrderCode(buyOrder.getBuyOrderCode());
                inhou.setMtrCode(buyOrder.getMtrCode());
                inhou.setMtrName(buyOrder.getMtrName());
                inhou.setBuyQuantity(buyOrder.getBuyQuantity());
                inhou.setSupplier(buyOrder.getSupplier());
                inhou.setOrderName(buyOrder.getOrderName());
            }
        }
        return buyInhous;
    }

    /**
     * 新增采购入库
     *
     * @param buyInhou 采购入库
     * @return 结果
     */
    @Override
    public int insertBuyInhou(BuyInhou buyInhou)
    {
        buyInhou.setCreateTime(DateUtils.getNowDate());
        return buyInhouMapper.insertBuyInhou(buyInhou);
    }

    /**
     * 修改采购入库
     *
     * @param buyInhou 采购入库
     * @return 结果
     */
    @Override
    public int updateBuyInhou(BuyInhou buyInhou)
    {
        buyInhou.setUpdateTime(DateUtils.getNowDate());
        return buyInhouMapper.updateBuyInhou(buyInhou);
    }

    /**
     * 批量删除采购入库
     *
     * @param ids 需要删除的采购入库主键
     * @return 结果
     */
    @Override
    public int deleteBuyInhouByIds(Long[] ids)
    {
        return buyInhouMapper.deleteBuyInhouByIds(ids);
    }

    /**
     * 删除采购入库信息
     *
     * @param id 采购入库主键
     * @return 结果
     */
    @Override
    public int deleteBuyInhouById(Long id)
    {
        return buyInhouMapper.deleteBuyInhouById(id);
    }


    /**
     * 入库
     * @param buyInHouVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult inHou(BuyInHouVo buyInHouVo) {
        BuyInhou buyInhou = buyInhouMapper.selectBuyInhouById(buyInHouVo.getId());

        BuyOrder buyOrder = buyOrderMapper.selectBuyOrderById(buyInhou.getBuyId());
        //判断库存剩余容量是否充足
        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(buyInHouVo.getHouId());
        if (houInfo.getHouRemainCapacity()  < buyOrder.getBuyQuantity()){
            return AjaxResult.error("库存剩余容量不足!");
        }

        //调取龙门,根据库位进行放料
        LongMenUtils.LongMenPutWithLocator(buyInHouVo.getLocatorInfoId());

        //更新库房剩余容量
        houInfo.setHouRemainCapacity(houInfo.getHouRemainCapacity() - buyOrder.getBuyQuantity());
        houInfoMapper.updateHouInfo(houInfo);

        //更新库位剩余容量
        LocatorInfo locatorInfo = locatorInfoMapper.selectLocatorInfoById(buyInHouVo.getLocatorInfoId());
        if (locatorInfo.getSerialCode() == null || StringUtils.isBlank(locatorInfo.getSerialCode())){//空库位
            locatorInfo.setSerialCode(buyOrder.getMtrCode());
        } else if (!locatorInfo.getSerialCode().equals(buyOrder.getMtrCode())) {
            return AjaxResult.error("物品不匹配!");
        }
        locatorInfo.setRemainCapacity(locatorInfo.getRemainCapacity() - buyOrder.getBuyQuantity());
        locatorInfoMapper.updateLocatorInfo(locatorInfo);

        //更新物料库存
        List<MtrStock> stocks = mtrStockMapper.selectMtrStockByMtrCode(buyOrder.getMtrCode());

        if(stocks.size() == 0){//若库存没有该原料，则新增
            MtrStock stock = new MtrStock();
            stock.setHouId(buyInHouVo.getHouId());
            stock.setMtrCode(buyOrder.getMtrCode());
            stock.setMtrName(buyOrder.getMtrName());
            stock.setMtrQuantity(buyOrder.getBuyQuantity());
            mtrStockMapper.insertMtrStock(stock);

        }else {
            MtrStock mtrStock = stocks.stream().max(Comparator.comparing(MtrStock::getMtrQuantity)).get();
            mtrStock.setMtrQuantity(mtrStock.getMtrQuantity() + buyOrder.getBuyQuantity());
            mtrStockMapper.updateMtrStock(mtrStock);
        }
        //更新入库状态
        buyInhou.setInspcType(1L);
        buyInhouMapper.updateBuyInhou(buyInhou);



        return AjaxResult.success("入库成功!");
    }
}
